// components/my-commodity/my-commodity.js
var common = require("../../utils/public.js") //引入public.js脚本
import WxValidate from "../../utils/wxValidate.js";
var api = require("../../utils/api");
var cf = require("../../config.js");
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    array: [], //城市列表
    selectorIndex: 0, //城市
    // switch1Checked: 0,//默认
    mapAddr: {}, //map返回地址列表
    obj: {},
    form: {
      linkMan: "", //收货人
      linkTel: "", //联系电话
      addr: "", //收货地址
      buildNo: "", //详细地址
      newAdd: null
    }
  },
  lifetimes: {
    // 提示信息/城市接口
    attached() {
        let that = this;
        //城市接口
        api.cityList({
          page: {
            size: 6,
            current: 0,
            isStartPage: false,
            pages: 1,
          }
        }).then(res => {
          if (res.result) {
            let array = that.data.array;
            let selector = res.object.records;
            selector.forEach((v) => {
              array.push(v.name)
            })
            that.setData({
              array
            })
          }
        })
        // 信息
        let rules = {
          linkMan: {
            required: true,
            minlength: 2
          },
          linkTel: {
            required: true,
            tel: true,
          },
          addr: {
            required: true,
            minlength: 2
          },
          buildNo: {
            required: true,
            minlength: 3
          }
        }
        let message = {
          linkMan: {
            required: "请填写收货人",
            minlength: "请填写正确收货人"
          },
          linkTel: {
            required: "请输入手机号",
            tel: "请输入正确手机号",
          },
          addr: {
            required: "请填写收货地址",
            minlength: "请填写正确收货地址"
          },
          buildNo: {
            required: "请填写详细地址",
            minlength: "请重新填写详细地址"
          }
        }
        this.WxValidate = new WxValidate(rules, message)
      
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    
    //保存
    handleSubmit: common.throttleFunc(function (e) {
      let that = this;
      //收货人，电话，地址，详细地址
      let {
        linkMan,
        linkTel,
        addr,
        buildNo
      } = that.data.form;
      //默认 返回地址
      let {
        switch1Checked,
        mapAddr
      } = that.data;
      // 经纬度 区域 区域编码
      let {
        latitude,
        longitude,
        district,
        adcode
      } = mapAddr
      if (!this.WxValidate.checkForm(e)) {
        let err = this.WxValidate.errorList[0]
        this.showModal(err);
        return false;
      }

      // 调用接口
      api.formSubmit({
        addr: e.detail.value.addr, //地址
        linkman: e.detail.value.linkMan, //收货人
        linkTel: e.detail.value.linkTel, //收货人电话
        latitude: latitude, //纬度
        longitude: longitude, //经度
        cityAddr: district, // 所属区
        cityCode: adcode, // 区码
        buildNo: e.detail.value.buildNo, //详细地址【看后台参数***】
        // isFlag: switch1Checked,//默认地址状态【看后台参数***】
        userInfoId: wx.getStorageSync("openId"), //用户id
        cityId: wx.getStorageSync("cityId"), //城市编号
        addressType: 2 //个人
      })
        .then(res => {
          if (res.result) {
            if (this.data.newAdd == null) {
              wx.request({
                url: `${cf.url}/admin/user/loginUserType`,
                data: {
                  openid: wx.getStorageSync("openId"),
                  authUserId: '-1',
                  type: 2,
                  shopId: '-1'
                },
                header: {
                  'content-type': 'application/json',
                },
                method: 'POST',
              })
            }
            wx.showToast({
              title: '提交成功',
              duration: 1500,
              success: () => {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
            })
          }
        })
    }),
    mapAddr() {
      let {
        addr
      } = this.data.form;
      let {
        mapAddr
      } = this.data;
      let addressInfo = mapAddr.addressInfo;
      if (addressInfo && JSON.stringify(addressInfo) != "{}") {
        //省市区...
        let address = mapAddr.newWhere;
        let mapAdd = "form.addr";
        this.setData({
          [mapAdd]: address
        })
      }
    },
    // 跳转地图页面
    maps() {
      let {
        array,
        selectorIndex
      } = this.data;
      if (JSON.stringify(this.data.mapAddr) === '{}') {
        wx.navigateTo({
          url: '/components/my-map/my-map?citys=' + array[selectorIndex],
        })
      } else {
        const {
          latitude,
          longitude,
          newWhere
        } = this.data.mapAddr
        wx.navigateTo({
          url: `/components/my-map/my-map?latitude=${latitude}&longitude=${longitude}&title=${newWhere}&citys=${array[selectorIndex]}`,
        })
      }
    },
    //城市选择
    bindPickerChange: function (e) {
      let addr = "form.addr";
      this.setData({
        selectorIndex: e.detail.value,
        [addr]: "",
        mapAddr: {}
      })
    },
    //默认设置
    switch1Change: function (e) {
      this.setData({
        switch1Checked: e.detail.value ? 1 : 0
      })
    },
    // 错误信息
    showModal(err) {
      wx.showModal({
        content: err.msg,
        showCancel: false
      })
    },
    
    
  },
  
})
